const fs = require('fs')
const path = require('path')

const regCss = /<style>[\s\S]*<\/style>/

const regScript = /<script>[\s\S]*<\/script>/

fs.readFile(path.join(__dirname, './index.html'), 'utf-8', (err, data) => {
    if (err) return console.log('读取文件失败');
    resolveCss(data)
    resolveJs(data)
    resolveHtml(data)
})

// css处理函数
function resolveCss(t) {
    let cssResult = regCss.exec(t)
    let cssResult1 = cssResult[0].replace('<style>', '').replace('</style>', '')
    fs.writeFile(path.join(__dirname, './files/clock/index.css'), cssResult1, (err) => {
        if (err) return console.log('写入文件失败');
    })
}

// js处理函数
function resolveJs(t) {
    let jsResult = regScript.exec(t)
    let jsResult1 = jsResult[0].replace('<script>', '').replace('</script>', '')
    fs.writeFile(path.join(__dirname, './files/clock/index.js'), jsResult1, err => {
        if (err) return console.log('写入js文件失败');
    })
}

// 使用外联对index.html文件进行替换操作
function resolveHtml(t) {
    let htmlResult = t.replace(regCss, '<link rel="stylesheet" href="./index.css">').replace(regScript, '<script src="./index.js"></script>')
    fs.writeFile(path.join(__dirname, './files/clock/index.html'), htmlResult, err => {
        if (err) return console.log('html写入失败');
    })
}
